from django.http import JsonResponse
from django.views import View
from rest_framework.generics import ListAPIView, RetrieveAPIView, UpdateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from activities.serializers import GatheringListSerializer, GatheringDetailSerialzer
from activities.models import Gathering
from rest_framework.response import Response
from django.views import View

# Create your views here.
class ListView(ListAPIView):
    pagination_class = None
    queryset = Gathering.objects.all()
    serializer_class = GatheringListSerializer

    # 活动列表
    def get(self,request):
        results = super().get(self, request)
        count = Gathering.objects.count()
        data = {
            'count': count,
            'next': None,
            'previous': None,
            'results': results.data
        }
        return Response(data)


class DateilsView(RetrieveAPIView):
    queryset = Gathering.objects.all()
    serializer_class = GatheringDetailSerialzer


class ApplyView(APIView):
    permission_classes = [IsAuthenticated, ]
    def post(self, request, pk):
        gathering = Gathering.objects.get(id=pk)
        user = request.user
        gathering_user = gathering.users.all()
        if user not in gathering_user:
            gathering.users.add(user)
            data = {
                'message': '报名成功',
                'success': True
            }
            return Response(data)
        else:
            data = {
                'message': '您已经报过了，请不要重复报名',
                'success': False
            }
            return Response(data)

